package com.exam.serviceimpl;

import com.exam.entity.DataDictionaryType;
import com.exam.mapper.DataDictionaryTypeRepository;
import com.exam.service.DataDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DataDictionaryTypeServiceImpl implements DataDictionaryTypeService {

    // 注入
    @Autowired
    private DataDictionaryTypeRepository repository;

    // 创建
    @Override
    public DataDictionaryType create(DataDictionaryType dataDictionaryType) {
        return repository.save(dataDictionaryType);
    }

    // 更新
    @Override
    public DataDictionaryType update(Long id, DataDictionaryType dataDictionaryType) {
        DataDictionaryType existing = repository.findById(id)
                .orElseThrow(() -> new RuntimeException("Data Dictionary Type not found"));
        existing.setName(dataDictionaryType.getName());
        existing.setDescription(dataDictionaryType.getDescription());
        return repository.save(existing);
    }

    // 删除
    @Override
    public void delete(Long id) {
        repository.deleteById(id);
    }

    // 查询
    @Override
    public DataDictionaryType findById(Long id) {
        return repository.findById(id)
                .orElseThrow(() -> new RuntimeException("Data Dictionary Type not found"));
    }

    // 查询所有
    @Override
    public List<DataDictionaryType> findAll() {
        return repository.findAll();
    }

}